package org.mspring.mlog.web.module.web.ucenter.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Message;
import org.mspring.mlog.service.MessageService;
import org.mspring.mlog.web.query.MessageQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 站内消息
 * 
 * @author Gao Youbo
 * @since 2013年7月22日
 */
@Widget
@RequestMapping("/u/{username}/admin/message")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class UCenterAdmin_MessageWidget extends Abstract_UCenterAdminWidget {

    @Autowired
    private MessageService messageService;

    @RequestMapping({ "/", "" })
    public String list(@ModelAttribute Page messagePage, @ModelAttribute Message message, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (messagePage == null) {
            messagePage = new Page();
        }
        if (queryParams == null) {
            queryParams = new HashMap();
        }
        String filter = RequestUtils.getRequestParameterAsString(request, "filter");
        if (StringUtils.isBlank(filter)) {
            filter = "all";
        }
        if ("unread".equals(filter)) {
            queryParams.put("readed", false);
        }
        queryParams.put("receiver.id", spaceUser.getId());
        messagePage = messageService.listPage(new MessageQueryCriterion(queryParams), messagePage);
        model.addAttribute("messagePage", messagePage);
        model.addAttribute("filter", filter);
        return "skin:/ucenter/admin/message/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity deleteMessage(HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        try {
            Long id = RequestUtils.getRequestParameterAsLong(request, "id");
            messageService.deleteMessage(spaceUser.getId(), id);
            rsp.setSuccess(true);
            return rsp;
        } catch (Exception e) {
            // TODO: handle exception
            rsp.setSuccess(false);
            rsp.setMessage(e.getMessage());
            return rsp;
        }
    }

    /**
     * 清理用户消息的未读状态
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/setReaded")
    @ResponseBody
    public ResponseEntity setReaded(HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        try {
            messageService.setReadedMessage(spaceUser.getId());
            rsp.setSuccess(true);
            return rsp;
        } catch (Exception e) {
            // TODO: handle exception
            rsp.setSuccess(false);
            rsp.setMessage(e.getMessage());
            return rsp;
        }
    }
}
